package com.lkm.market2_0.m_imple.steelyarddata;

import com.google.gson.JsonObject;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskManager;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelyardValueEvent;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.netapi.AppApi;
import com.lkm.market2_0.task.DefaultTaskExecutor;
import com.lkm.market2_0java.help.GsonHelp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



  class RegBalanceTask extends TaskAdapter<SteelyardValue, String, Boolean> {

    public static <T extends Task> List<T> getAllTask(String taskId) {
        TaskManager taskManager = ExpandConfig.instance.getTaskManager2();
        int[] runs = taskManager.getRunTask(taskId);
        if (runs != null && runs.length != 0) {
            List<T> tasks = new ArrayList<>(runs.length);
            Task task = null;
            for (int se : runs) {
                task = taskManager.getRunTask(se);
                if (task != null) {
                    tasks.add((T) task);
                }
            }

            return tasks;
        }
        return null;
    }

    

    public static void waitValue() {
        if(ExpandConfig.instance.isUI())return;


        LinkBus.registerReceiver(new ObserverAutoNext<OnSteelyardValueEvent>() {
            @Override
            public void onReceiver(OnSteelyardValueEvent data) {

                start(data.getSteelyardValue());
            }
            @Override
            public Class getBusEventClass() {
                return OnSteelyardValueEvent.class;
            }
        });







//                steelyardValue.steelyardMac = data.getSteelyardMac();








    }

    public static RegBalanceTask start(SteelyardValue steelyardValue) {
        RegBalanceTask regBalanceTask = new RegBalanceTask();
        regBalanceTask.action();
        regBalanceTask.setParam(steelyardValue);
        regBalanceTask.execTask();
        return regBalanceTask;
    }

    public RegBalanceTask() {
        super(new DefaultTaskExecutor(true), RegBalanceTask.class.getName(), ExpandConfig.instance.getTaskManager2());
    }

    @Override
    public void doExecuting(SteelyardValue param) {
        super.doExecuting(param);

        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                ayncdoExecuting();
            }
        });

    }



    private void ayncdoExecuting() {


        SteelyardValue steelyardValue = getParam();

        Map<String, Object> stringStringMap = GsonHelp.fromJson(GsonHelp.toJson(steelyardValue),new com.google.gson.reflect.TypeToken<Map<String, String>>() {}.getType());


        StatusInfo statusInfo = null;
        SteelyardStatusManagerModule statusManagerModule = (SteelyardStatusManagerModule) ExpandConfig.instance.getModuleManager().peekModule(SteelyardStatusManagerModule.class);
        if (statusManagerModule != null ) {
            statusInfo = statusManagerModule.getStatusInfoBySteelyardMac(steelyardValue.steelyardMac);
        }

        JsonObject configjson = new JsonObject();
        configjson.addProperty("mac",steelyardValue.steelyardMac);

        if(statusInfo != null){
            configjson.addProperty("romv",statusInfo.getVersion());
            steelyardValue.exit_factory_id = statusInfo.getNumno();
        }
        
//        configjson.addProperty("appv", AppHelp.getLocalVersionName());

        stringStringMap.put("configjson",configjson.toString());

        stringStringMap.put("moid",15);
        stringStringMap.put("exit_factory_id",steelyardValue.exit_factory_id);
        stringStringMap.put("balance_no",steelyardValue.steelyardMac);

        String orgId = "";
        String ukey = steelyardValue.steelyardMac;

        String json = AppApi.regBalanceTask(orgId,ukey,stringStringMap, this);

        ResponEntity<Void> responEntity = ResponEntity.fromJson(json, null, "");

        if (responEntity.isSuccess()) {
            complete(responEntity.isSuccess());
            steelyardValue.state = "登记ok";
        } else {
            complete(false);
            steelyardValue.state = "登记fail";
        }


    }




}